1271B - Blocks - CodeForces Solution


greedy math *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define pob pop_back()
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define prq priority_queue<ll>
#define psq priority_queue<ll,vector<ll>,greater<ll>>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repi(i, b, a) for (ll i = b; i >= a; i--)
#define input(x) {for(auto &i : x) cin>>i;}
#define input1(x) {rep(i,1,n+1) cin>>x[i];}
#define output(x) {for(auto &i : x) cout<<i<<" ";}
#define inf INT_MAX
#define minf INT_MIN
#define MOD 1000000007
#define all(x) x.begin(),x.end()
#define nline "\n"
#define yes cout<<"YES"<<nline;
#define no cout<<"NO"<<nline;


// #define priyanshu_2401 1


//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Debug
#ifdef priyanshu_2401
#define debug(x) cerr << #x<<" "; cerr<<x<<" "; cerr << endl;
#else
#define debug(x);
#endif

// Operator overloads
template<typename T1, typename T2> // cin >> pair<T1, T2>
istream& operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
template<typename T> // cin >> vector<T>
istream& operator>>(istream &istream, vector<T> &v){for (auto &it : v)cin >> it;return istream;}
template<typename T1, typename T2> // cout << pair<T1, T2>
ostream& operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
template<typename T> // cout << vector<T>
ostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; }

// Mathematical functions
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);} //__gcd 
ll lcm(ll a, ll b){return (a/gcd(a,b)*b);}
ll powermod(ll x, ll y, ll p){ll res = 1;x = x % p;if (x == 0) return 0;while (y > 0){if (y & 1)res = (res*x) % p;y = y>>1;x = (x*x) % p;}return res;}
ll modMultiply(ll x,ll y) {return ((x%MOD)*(y%MOD))%MOD;}
ll modAdd(ll x,ll y) {return ((x%MOD)+(y%MOD))%MOD;}
ll modSubtract(ll x,ll y) {return ((x%MOD)-(y%MOD)+MOD)%MOD;}
ll lengthOfNumber(ll x){ll count=0;while(x>0){count++;x/=10;}return count;}
 
vector<ll> fac(1002),inv(1002);
ll ncr(ll n,ll r){
    if(n<r) return 0;
    ll ans = modMultiply(modMultiply(fac[n],inv[r]),inv[n-r]);
    return ans;
}

//Graph-dfs
// bool gone[MN];
// vector<int> adj[MN];
// void dfs(int loc){
//     gone[loc]=true;
//     for(auto x:adj[loc])if(!gone[x])dfs(x);
// }

//Sorting
bool sorta(const pair<int,int> &a,const pair<int,int> &b){return (a.second < b.second);}
bool sortd(const pair<int,int> &a,const pair<int,int> &b){return (a.second > b.second);}

//Bits
string decToBinary(int n){string s="";int i = 0;while (n > 0) {s =to_string(n % 2)+s;n = n / 2;i++;}return s;}
ll binaryToDecimal(string n){string num = n;ll dec_value = 0;int base = 1;int len = num.length();for(int i = len - 1; i >= 0; i--){if (num[i] == '1')dec_value += base;base = base * 2;}return dec_value;}

//Check
bool isPrime(ll n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
bool isPowerOfTwo(int n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));}
bool isPerfectSquare(ll x){if (x >= 0) {ll sr = sqrt(x);return (sr * sr == x);}return false;}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


//code goes here

void solve()
{
	ll n;
	cin>>n;
	string s;
	cin>>s;
	
	ll b=0,w=0;

	for(auto &ch:s)
	{
		if(ch=='B') b++;
    	if(ch=='W') w++;
	}
	
	if(b&1 && w&1)
	{
		cout<<-1<<nline;
		return;
	}

	vll res;
	
	if(w&1)
	{
		ll i = 0;
		while(i<n)
		{
			if(s[i]=='B')
			{
				if(s[i+1]=='W')
				{
					res.pb(i+1);
					s[i+1] = 'B';
				}
				else
				{
					res.pb(i+1);
					s[i+1]='W';
				}
			}
			i++;
		}
	}
	else
	{
		ll i = 0;
		while(i<n)
		{
			if(s[i]=='W')
			{
				if(s[i+1]=='B')
				{
					res.pb(i+1);
					s[i+1]='W';
				}
				else
				{
					res.pb(i+1);
					s[i+1]='B';
				}
			}
			i++;
		}
	}

	cout<<res.size()<<nline;
	cout<<res<<nline;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    cout.tie(NULL);
	// ll t;
	// cin>>t;
	// while(t--)
	solve();
	return 0;
}


Comments

Submit
0 Comments
More Questions

1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings